﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports Utilerias
Imports WSFactory
Imports MySql.Data.MySqlClient
Public Class CIeduDAL
    Private cadConex As String = ""

    Public Sub New(ByVal pCadConex As String)
        Me.cadConex = pCadConex
    End Sub

    Public Function actualizaIeduConfig(ByVal pIedu As CIeduBE) As Boolean
        Dim boolActualizado As Boolean = False
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE configcomplementos " & _
                                            "SET Valor = @Valor " & _
                                            "WHERE CveComp = 'iedu' and CveConfig = @cveConfig; "
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("@Valor", pIedu.autRVOE_Preescolar)
                    myComm.Parameters.AddWithValue("@cveConfig", "Preescolar")
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() = 1) Then
                        myComm.Parameters.Clear()
                        myComm.Parameters.AddWithValue("@Valor", pIedu.autRVOE_Primaria)
                        myComm.Parameters.AddWithValue("@cveConfig", "Primaria")
                        If (myComm.ExecuteNonQuery() = 1) Then
                            myComm.Parameters.Clear()
                            myComm.Parameters.AddWithValue("@Valor", pIedu.autRVOE_Secundaria)
                            myComm.Parameters.AddWithValue("@cveConfig", "Secundaria")
                            If (myComm.ExecuteNonQuery() = 1) Then
                                myComm.Parameters.Clear()
                                myComm.Parameters.AddWithValue("@Valor", pIedu.autRVOE_Profesional)
                                myComm.Parameters.AddWithValue("@cveConfig", "Profesionaltecnico")
                                If (myComm.ExecuteNonQuery() = 1) Then
                                    myComm.Parameters.Clear()
                                    myComm.Parameters.AddWithValue("@Valor", pIedu.autRVOE_Bachillerato)
                                    myComm.Parameters.AddWithValue("@cveConfig", "BachilleratoEquiv")
                                    If (myComm.ExecuteNonQuery() = 1) Then
                                        boolActualizado = True
                                    End If
                                End If
                            End If
                        End If
                    End If
                End Using
            End Using

            Return boolActualizado
        Catch ex As Exception
            Throw
        Finally
            boolActualizado = Nothing
        End Try
    End Function

    Public Function actualizaIeduComp(ByVal pIedu As CIeduBE) As Boolean
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE complementos " & _
                                            "SET Status = @Status " & _
                                            "WHERE CveComp = 'iedu'; "
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    If (pIedu.Status = CIeduBE.EstatusIEDU.Activa) Then
                        myComm.Parameters.AddWithValue("@Status", "S")
                    Else
                        myComm.Parameters.AddWithValue("@Status", "N")
                    End If
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() = 1) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function getIedu() As CIeduBE
        Dim tmpIedu As CIeduBE
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT complementos.Status, " & _
                                                "configcomplementos.CveComp, " & _
                                                "configcomplementos.CveConfig, " & _
                                                "configcomplementos.Valor " & _
                                            "FROM complementos " & _
                                                "INNER JOIN configcomplementos " & _
                                                        "ON (complementos.CveComp = configcomplementos.CveComp) " & _
                                            "WHERE configcomplementos.CveComp = 'iedu' ;"
                    myComm.CommandType = CommandType.Text
                    myConn.Open()
                    Using myDR As MySqlDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            tmpIedu = CIeduFact.Build(myDR)
                        Else
                            tmpIedu = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpIedu

        Catch ex As Exception
            Throw
        Finally
            tmpIedu = Nothing
        End Try
    End Function

End Class
